package constants

import (
	"os"
	"path/filepath"
	"sjy/customcmd/src/utils"
)

var (
	AppRoot          = getAppRoot()
	GlobalConfigRoot = getGlobalConfigRoot()
	GlobalConfigPath = filepath.Join(GlobalConfigRoot, "config.yaml")
	VersionFilePath  = filepath.Join(GlobalConfigRoot, "latest.yaml")
	InfomationPath   = filepath.Join(GlobalConfigRoot, "infomation.json")
	LocalConfigRoot  = getLocalConfigRoot()
	LocalConfigPath  = filepath.Join(LocalConfigRoot, "config.yaml")
)

func getAppRoot() string {
	exePath, _ := os.Executable()
	if utils.IsPackage() {
		return filepath.Dir(exePath)
	}
	return filepath.Join(filepath.Dir(exePath), "..", "etc")
}

func getGlobalConfigRoot() string {
	exePath, _ := os.Executable()
	if utils.IsPackage() {
		return filepath.Join(filepath.Dir(exePath), "config")
	}
	return filepath.Join(filepath.Dir(exePath), "..", "etc", "config")
}

func getLocalConfigRoot() string {
	currentDir, _ := os.Getwd()
	if utils.IsPackage() {
		return filepath.Join(currentDir, ".tools")
	}
	exePath, _ := os.Executable()
	return filepath.Join(filepath.Dir(exePath), "..", "etc", ".tools")
}
